package blockchain.mystream;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MySocketNoExcept extends MySocketAbstract implements MyDataInputStreamInterfaceNoExcept, MyDataOutputStreamInterfaceNoExcept {
    public MySocketNoExcept(Socket socket) {
        try {
            super.socket = socket;
            super.in = new DataInputStream(socket.getInputStream());
            super.out = new DataOutputStream(socket.getOutputStream());
        }
        catch (IOException e) {
            System.err.println("Socket初始化发生错误: " + e.getMessage());
        }
    }
    public MySocketNoExcept(String ip, int port) {
        try {
            super.socket = new Socket(ip, port);
            super.in = new DataInputStream(socket.getInputStream());
            super.out = new DataOutputStream(socket.getOutputStream());
        }
        catch (IOException e) {
            System.err.println("Socket初始化发生错误: " + e.getMessage());
        }
    }

    @Override
    public DataInputStream getDataInputStream() {
        return in;
    }

    @Override
    public DataOutputStream getDataOutputStream() {
        return out;
    }

    @Override
    public void close() {
        try {
            socket.close();
        } catch (IOException e) {
            System.out.println("Socket关闭发生错误: " + e.getMessage());
        }
    }
}
